package ipad.asia.controller;

import ipad.asia.config.NewOrder;
import ipad.asia.entity.Consumer;
import ipad.asia.entity.Enterprise;
import ipad.asia.entity.Order;
import ipad.asia.entity.Product;
import ipad.asia.service.consumer.ConsumerFeign;
import ipad.asia.service.order.OrderFeign;
import ipad.asia.service.product.ProductFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class OrderController {
    @Autowired
    private OrderFeign orderFeign;
    @Autowired
    private ConsumerFeign consumerFeign;
    @Autowired
    private ProductFeign productFeign;
    @RequestMapping("/order/list")
    public String orderList(HttpServletRequest request, Model model){
        List<Order> orders = orderFeign.findByShopId((Integer) request.getSession().getAttribute("shopId"));
        List<Integer> consumerIds=new ArrayList<>();
        for(int i=0;i<orders.size();i++){
            if(!consumerIds.contains(orders.get(i).getConsumerId())){
                consumerIds.add(orders.get(i).getConsumerId());
            }
            else continue;
        }
        List<Consumer> consumers=new ArrayList<>();
       for(int i=0;i<consumerIds.size();i++){
            consumers.add(consumerFeign.getId(consumerIds.get(i)));
       }
       List<NewOrder> newOrders=new ArrayList<>();
       for(int i=0;i<orders.size();i++){
           for(int j=0;j<consumers.size();j++){
               System.out.println("orders.get(i).getConsumerId()"+orders.get(i).getConsumerId());
               System.out.println("consumers.get(j).getId()"+consumers.get(j).getId());
               if(orders.get(i).getConsumerId()==consumers.get(j).getId()){
                    NewOrder newOrder=new NewOrder();
                    newOrder.setOrder(orderFeign.findId(orders.get(i).getId()));
                    newOrder.setConsumer(consumerFeign.getId(consumers.get(j).getId()));
                    newOrders.add(newOrder);
               }
           }
       }
        model.addAttribute("newOrders",newOrders);
        return "orderList";
    }



    @RequestMapping("/order/detail")
    public String orderDetail(@RequestParam(value = "id",required = false)String id, Model model){
        System.out.println(id);
        Order order = orderFeign.findId(Integer.parseInt(id));
        Consumer consumer = consumerFeign.getId(order.getConsumerId());
        Product product = productFeign.getById(order.getProductId());
        NewOrder newOrder=new NewOrder();
        newOrder.setOrder(order);newOrder.setConsumer(consumer);newOrder.setProduct(product);
        model.addAttribute("newOrder",newOrder);
        return "orderDetail";
    }



    @RequestMapping("/order/find")
    public String orderFind(@RequestParam(value = "statue",required = false)String statue,
                            @RequestParam(value = "number",required = false)String number,
                            HttpSession session,Model model) {
        System.out.println("number:"+number);
        int shopId= (int) session.getAttribute("shopId");
        int result=4;
        List<Order> orders;
        if(number==null||number.equals("")||number.trim().length()==0) {
            if (statue.equals("订单状态")) {
               orders=orderFeign.findByShopId(shopId);
            }
            if (statue.equals("待付款")) {
                result=0;
            }
            if (statue.equals("待发货")) {
                result=1;
            }
            if (statue.equals("待收货")) {
                result=2;
            }
            if (statue.equals("待评价")) {
               result=3;
            }
            orders=orderFeign.findByShopIdAndStatues(shopId,result);
        }else {
           orders= orderFeign.findByShopIdAndIdAndStatues(shopId, Integer.parseInt(number),result);
        }
        List<Integer> consumerIds = new ArrayList<>();
        for (int i = 0; i < orders.size(); i++) {
            if (!consumerIds.contains(orders.get(i).getConsumerId())) {
                consumerIds.add(orders.get(i).getConsumerId());
            } else continue;
        }
        List<Consumer> consumers = new ArrayList<>();
        for (int i = 0; i < consumerIds.size(); i++) {
            consumers.add(consumerFeign.getId(consumerIds.get(i)));
        }
        //消费者和订单的参数合并在一个对象
        List<NewOrder> newOrders = new ArrayList<>();
        for (int i = 0; i < orders.size(); i++) {
            for (int j = 0; j < consumers.size(); j++) {
                System.out.println("orders.get(i).getConsumerId()" + orders.get(i).getConsumerId());
                System.out.println("consumers.get(j).getId()" + consumers.get(j).getId());
                if (orders.get(i).getConsumerId() == consumers.get(j).getId()) {
                    NewOrder newOrder = new NewOrder();
                    newOrder.setOrder(orderFeign.findId(orders.get(i).getId()));
                    newOrder.setConsumer(consumerFeign.getId(consumers.get(j).getId()));
                    newOrders.add(newOrder);
                }
            }
        }

        model.addAttribute("newOrders", newOrders);
        return "orderList";
    }

}
